Skip to content

Speed up Hexadecatree color insertion - #3291

Open
g4lb wants to merge 1 commit into
SixLabors:mainfrom
g4lb:perf/hexadecatree-descent
Open

g4lb wants to merge 1 commit into
SixLabors:mainfrom
g4lb:perf/hexadecatree-descent

Conversation

@g4lb

@g4lb g4lb commented Nov 30, 2026

Copy link
Copy Markdown

Prerequisites

  • I have written a descriptive pull-request title
  • I have verified that there are no overlapping pull-requests open
  • I have verified that I am following the existing coding patterns and practice as demonstrated in the repository. These follow strict Stylecop rules 👮.
  • I have provided test coverage for my change (where applicable)

Description

Hexadecatree.Node.AddColor used one recursive call for each tree level. It also calculated the luminance again at each level. This change uses a loop, and it calculates the luminance one time for each color. GetPaletteIndex also calculates the luminance one time.

Results (.NET 10, Apple M2 Max, 10 rounds, p < 0.0001):

Case Before After
HexadecatreeQuantizer, no dither, 189x175 photo 2.77 ms 2.04 ms
KnownQuantizers.Hexadecatree, same photo 3.54 ms 3.07 ms
KnownQuantizers.Hexadecatree, 256x256 gray 15.4 ms 4.09 ms
GIF encode, same photo 4.00 ms 3.61 ms

I also measured 40 image shapes and options. No case got slower.

All tests pass. A differential test against main on 1.2M random inputs found no difference in output.

@CLAassistant

CLAassistant commented Nov 30, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants